<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!--      添加分类按钮-->
      <el-row>
        <el-col>
          <el-button type="primary" @click="getParentNode">添加分类</el-button>
        </el-col>
      </el-row>
      <!--      对话框显示隐藏按钮-->
      <el-dialog
          title="添加分类信息"
          :visible.sync="addSortDialogVisible"
          width="50%"
          @close="clearAddSortForm"
      >
        <el-form :model="addSortForm" :rules="addSortRules" ref="addSortRuleForm" label-width="100px"
                 class="addSortruleForm">
          <el-form-item label="增加分类：" prop="cat_name">
            <el-input v-model="addSortForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父集分类：">
            <!--                el-cascader v-model绑定了选中对象，是双向绑 options是绑定到数据源，直接读到后端传过来的数据，change是监听函数，直接会对变化监听，其实就属于钩子函数，一般用他拿到变化的值，props是设置的配置项，value是选中所对应的ID，label是表现出的标签，children是两级菜单是按照何种方式关联-->
            <el-cascader
                v-model="selectedKey"
                :options="parentCateList"
                @change="parentCateChange"
                :props="cascaderProps"
                :clearable="true"
                change-on-select
            >
            </el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
    <el-button @click="addSortDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addSortDialogVisible = false">确 定</el-button>
  </span>
      </el-dialog>
      <!--      表格区域-->
      <tree-table class='treetable' :data="cateList" :columns="columns" :selection-type="false" :expand-type="false"
                  show-index border>
        <template v-slot:isOK="scope">
          <i class="el-icon-success" v-if="!scope.row.cat_deleted" style="color:lightgreen"></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <template v-slot:isLevel="scope">
          <el-tag v-if="scope.row.cat_level===0" size="mini">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level===1" size="mini">二级</el-tag>
          <el-tag type="warning" v-else size="mini">三级</el-tag>
        </template>
        <template v-slot:opt="scope">
          <el-button type="primary" size="mini" v-if="scope.row">编辑</el-button>
          <el-button type="danger" size="mini">删除</el-button>
        </template>
      </tree-table>
      <!--      分页区域-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1,2, 4, 8]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: "Cate",
    data() {
      return {
        addSortRules: {
          name: [
            {required: true, message: '请输入添加名称', trigger: 'blur'},
            {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
          ]
        },
        queryInfo: {
          type: 3,
          pagenum: 1,
          pagesize: 5
        },
        cateList: [],
        total: 0,
        columns: [
          {
            label: '分类名称',
            prop: 'cat_name',
          },
          {
            label: '是否有效',
            type: 'template',
            template: 'isOK',
          },
          {
            label: '级别',
            type: 'template',
            template: 'isLevel'
          },
          {
            label: '操作',
            type: 'template',
            template: 'opt'
          }],
        addSortDialogVisible: false,
        addSortForm: {
          cat_name: '',
          cat_pid: 0,
          cat_level: 0
        },
        parentCateList: [],
        cascaderProps: {
          value: 'cat_id',
          label: 'cat_name',
          children: 'children'
        },
        selectedKey: [],
      }
    },
    created() {
      // 在函数的生命周期函数上，千万不要忘记写this，否则找不到
      this.getCateList()
    },

    methods: {
      async getCateList() {
        const {data: res} = await this.$http.get('categories', {params: this.queryInfo})
        // console.log(res.data);
        if (res.meta.status !== 200) return this.$http.error('获取信息失败')
        this.cateList = res.data.result
        this.total = res.data.total
      },
      // 用函数绑定最新的页面数据数量
      handleSizeChange(pageSize) {
        this.queryInfo.pagesize = pageSize
        this.getCateList()
      },
      handleCurrentChange(pageNum) {
        this.queryInfo.pagenum = pageNum
        this.getCateList()
      },
      changeSortVisible() {
        this.addSortDialogVisible = true
      },
      async getParentNode() {
        const {data: res} = await this.$http.get('categories', {params: {type: 2}})
        if (res.meta.status !== 200) {
          return this.$message.error('获取腹肌数据失败')
        }
        this.parentCateList = res.data
        console.log(this.parentCateList);
        this.changeSortVisible()
      },
      parentCateChange() {
        // console.log(this.selectedKey);
        if (this.selectedKey.length > 0) {
          this.parentCateList.cat_pid = this.selectedKey[
          this.selectedKey.length - 1
              ]
          this.parentCateList.cat_level = this.selectedKey.length
          return
        } else {
          this.parentCateList.cat_pid = 0
          this.parentCateList.cat_level = 0
        }
      },
      clearAddSortForm() {
        this.$refs.addSortRuleForm.resetFields()
        this.selectedKey = []
        this.addSortForm.cat_pid = 0
        this.addSortForm.cat_level = 0
      }


    }
  }
</script>

<style scoped>
  .treetable {
    margin-top: 15px;
  }

  .el-cascader {
    width: 100%;
  }

</style>